﻿

CREATE PROCEDURE [dbo].[amix_GetObiecteInventar]
      @CodUnitate smallint,
      @FiltruDep varchar(8000),
      @FiltruCont varchar(8000),
      @FiltruSursaFinantare varchar(8000),
      @Luna smallint,
      @Anul smallint
AS
BEGIN
      SET NOCOUNT ON;
      DECLARE @PerCurenta decimal(10)
      SELECT @PerCurenta = AnulCurent*100+LunaCurenta FROM AmixSetup WHERE CodUnitate=@CodUnitate
      DECLARE @Arhiva bit

      SET @Arhiva=0
      
      IF @PerCurenta>@Anul*100+@Luna
            SET @Arhiva= 1
            
            
    IF @Arhiva=1
    BEGIN
            PRINT 'Arhiva'
            SELECT ar.[IDMijlocFix]
			  ,ar.[NrInventar]
			  ,ar.[ContAmortizare]
			  ,ar.[StocFaptic]
			  ,ar.[IDClasificare]
			  ,ar.[DataIntrare]
			  ,ar.[DataIesire]
			  ,ar.[CodUnitate]
			  ,ar.[IDDepartament]
			  ,ar.[IDStare]
			  ,ar.[ContImobilizare]
			  ,ar.[ContCheltuiala]
			  ,ar.[DurataAmortizare]
			  ,ar.[RegimAmortizare]
			  ,ar.[ValoareInventar]
			  ,ar.[PunereInFunctiune]
			  ,ar.[Compus]
			  ,ar.[Blocat]
			  ,ar.[IDGestionar]
			  ,ar.[ContTransfer]
			  ,ar.[ValoareAmortizata]
			  ,ar.[LuniAmortizate]
			  ,ar.[AmortizareLuna]
			  ,ar.[AmortizareAn]
			  ,ar.[InFolosinta]
			  ,ar.[Categorie]
			  ,ar.[IDUnitateMasura]
			  ,ar.[ContTransferForSuperior]
			  ,ar.[IDJurnal]
			  ,ar.[ValoareIntrataTransfer]
			  ,ar.[CantitateOI]
			  ,ar.[Denumire]
			  ,ar.[IDSursaFinantare]
			,um.Prescurtare UMDenumire,dp.Nume LocDepozitare,dp2.Nume NumeGestiune FROM AmixRegistruBK ar 
            LEFT OUTER JOIN UnitatiMasura um ON ar.IDUnitateMasura = um.IDUnitateMasura
            LEFT OUTER JOIN Departamente dp ON ar.IDDepartament=dp.IDDepartament AND dp.CodUnitate = ar.CodUnitate
            LEFT OUTER JOIN   Departamente dp2 ON dp.IDDepartParinte = dp2.IDDepartament AND dp.CodUnitate = dp2.CodUnitate
            Where ar.CodUnitate = @CodUnitate AND ar.Compus >=3 
            AND
            (LEN(LTRIM(RTRIM(@FiltruDep)))=0 OR (ar.IDDepartament IN (SELECT fart.value FROM dbo.SplitIntParameters(@FiltruDep,',') fart))) AND
            (LEN(LTRIM(RTRIM(@FiltruCont)))=0 OR (COALESCE(ar.ContTransferForSuperior,-1) IN (SELECT fart.value FROM dbo.SplitIntParameters(@FiltruCont,',') fart))) aND
            (LEN(LTRIM(RTRIM(@FiltruSursaFinantare)))=0 OR (COALESCE(ar.IDSursaFinantare,-1) IN (SELECT fart.value FROM dbo.SplitIntParameters(@FiltruSursaFinantare,',') fart)))
            AND (YEAR([DataIntrare])*100 + MONTH([DataIntrare])) <= (@Anul*100 + @Luna)
            AND Luna=@Luna AND An=@anul
            AND ar.InFolosinta=1
            RETURN
    END
    -- Insert statements for procedure here
      SELECT ar.[IDMijlocFix]
			  ,ar.[NrInventar]
			  ,ar.[ContAmortizare]
			  ,ar.[StocFaptic]
			  ,ar.[IDClasificare]
			  ,ar.[DataIntrare]
			  ,ar.[DataIesire]
			  ,ar.[CodUnitate]
			  ,ar.[IDDepartament]
			  ,ar.[IDStare]
			  ,ar.[ContImobilizare]
			  ,ar.[ContCheltuiala]
			  ,ar.[DurataAmortizare]
			  ,ar.[RegimAmortizare]
			  ,ar.[ValoareInventar]
			  ,ar.[PunereInFunctiune]
			  ,ar.[Compus]
			  ,ar.[Blocat]
			  ,ar.[IDGestionar]
			  ,ar.[ContTransfer]
			  ,ar.[ValoareAmortizata]
			  ,ar.[LuniAmortizate]
			  ,ar.[AmortizareLuna]
			  ,ar.[AmortizareAn]
			  ,ar.[InFolosinta]
			  ,ar.[Categorie]
			  ,ar.[IDUnitateMasura]
			  ,ar.[ContTransferForSuperior]
			  ,ar.[IDJurnal]
			  ,ar.[ValoareIntrataTransfer]
			  ,ar.[CantitateOI]
			  ,ar.[Denumire]
			  ,ar.[IDSursaFinantare],um.Prescurtare UMDenumire,dp.Nume LocDepozitare,dp2.Nume NumeGestiune FROM AmixRegistru ar 
      LEFT OUTER JOIN UnitatiMasura um ON ar.IDUnitateMasura = um.IDUnitateMasura
      LEFT OUTER JOIN Departamente dp ON ar.IDDepartament=dp.IDDepartament AND dp.CodUnitate = ar.CodUnitate
      LEFT OUTER JOIN   Departamente dp2 ON dp.IDDepartParinte = dp2.IDDepartament AND dp.CodUnitate = dp2.CodUnitate
      Where ar.CodUnitate = @CodUnitate AND ar.Compus >=3 AND
      (LEN(LTRIM(RTRIM(@FiltruDep)))=0 OR (ar.IDDepartament IN (SELECT fart.value FROM dbo.SplitIntParameters(@FiltruDep,',') fart))) AND
      (LEN(LTRIM(RTRIM(@FiltruCont)))=0 OR (COALESCE(ar.ContTransferForSuperior,-1) IN (SELECT fart.value FROM dbo.SplitIntParameters(@FiltruCont,',') fart))) aND
      (LEN(LTRIM(RTRIM(@FiltruSursaFinantare)))=0 OR (COALESCE(ar.IDSursaFinantare,-1) IN (SELECT fart.value FROM dbo.SplitIntParameters(@FiltruSursaFinantare,',') fart)))
      AND (YEAR([DataIntrare])*100 + MONTH([DataIntrare])) <= (@Anul*100 + @Luna)
    AND ar.InFolosinta=1
END